home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / external / rpc / rpc_tout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-08  |  4.9 KB  |  206 lines

  1. /* rpc_tout.c -
  2.  *     This file contains routines that are used to maintain the
  3.  *     server Tout information. This includes the establishment of 
  4.  *     a linked list that maintains a list of output lines along with
  5.  *     the flags for the lines. For information on these flags, 
  6.  *     consult the IDL ADG.
  7.  */
  8.  
  9. /*  
  10.   Copyright (c) 1988-1997, Research Systems Inc.  All rights reserved.
  11.   This software includes information which is proprietary to and a
  12.   trade secret of Research Systems, Inc.  It is not to be disclosed
  13.   to anyone outside of this organization. Reproduction by any means
  14.   whatsoever is  prohibited without express written permission.
  15.  */
  16.  
  17. static char rcsid[] = "$Id: rpc_tout.c,v 1.2 1997/01/18 08:23:00 ali Exp $";
  18.  
  19.  
  20. #include "idl_rpc.h"
  21.  
  22. /*
  23.  * Declare types for the output list. This is a simple link list of 
  24.  * output strings.
  25.  */
  26.  
  27. typedef struct rpc_tout_node {    /* node of the list */
  28.    struct rpc_tout_node   * next;
  29.    struct rpc_tout_node   * prev;
  30.    char  *data;
  31. }RPC_TOUT_NODE;
  32.  
  33. static struct rpc_tout_head {    /* head of the list. */
  34.    int      max_lines;
  35.    int      n_lines;
  36.    struct rpc_tout_node  *head;
  37.    struct rpc_tout_node  *tail;
  38. }   lHead = {100, 0, 0, 0};
  39.       
  40.  
  41. /*************************************************************
  42.  * IDL_RPCOutListInit()
  43.  *
  44.  * Purpose:
  45.  *     Initailizes the head of the list for the list
  46.  *
  47.  * Parameters:
  48.  *    maxLines - the maximum number of lines used in the list.
  49.  */
  50. void IDL_RPCOutListInit( int maxLines )
  51. {
  52.    lHead.max_lines = (maxLines <= 0 ? 200 : maxLines);
  53.    lHead.head = NULL;
  54.    lHead.tail = NULL;
  55. }
  56. /************************************************************
  57.  * IDL_RPCOutListDequeue()
  58.  *
  59.  * Purpose:
  60.  *     This routine is used to remove the end item from the 
  61.  *     Output list.
  62.  */
  63. char *IDL_RPCOutListDequeue(void)
  64. {
  65.    char * pData;
  66.    RPC_TOUT_NODE *pNode;
  67.  
  68. /*
  69.  * are there any items in the list?
  70.  */
  71.    if(lHead.n_lines == 0)
  72.       return (char*)NULL;
  73. /*
  74.  * Get the end data and move up the prev pointer.
  75.  */
  76.    pData = lHead.tail->data;
  77.    pNode = lHead.tail;
  78.    lHead.tail = (--lHead.n_lines == 0 ? NULL : pNode->prev);
  79.    free((char*)pNode);        /* free node */
  80.    return pData;
  81. }
  82. /************************************************************
  83.  * IDL_RPCOutListPop()
  84.  *
  85.  * Purpose:
  86.  *     This routine is used to pop the first element of the top 
  87.  *     of the output list.
  88.  */
  89. char *IDL_RPCOutListPop(void)
  90. {
  91.    char  *pData;
  92.    RPC_TOUT_NODE  *pNode;
  93.  
  94.    if(lHead.n_lines == 0)
  95.       return (char*)NULL; /* no need to continue */
  96. /*
  97.  * Pop the list.
  98.  */
  99.    pData = lHead.head->data;
  100.    pNode = lHead.head;
  101.    lHead.head = pNode->next;
  102.    if(--lHead.n_lines == 0)
  103.       lHead.tail = NULL;
  104.    else 
  105.       lHead.head->prev = NULL;
  106.    free((char*)pNode);
  107.    return pData;
  108. }
  109. /************************************************************
  110.  * IDL_RPCOutListAdd()
  111.  * 
  112.  * Purpose:
  113.  *     This function is used to add one node of data to the list
  114.  */
  115. char * IDL_RPCOutListAdd( char *pData )
  116. {
  117.    RPC_TOUT_NODE *pNode;
  118.    char * pOldData = (char*)NULL;
  119. /*
  120.  * Alloc a new node
  121.  */
  122.    pNode = (RPC_TOUT_NODE*)malloc((unsigned)sizeof(RPC_TOUT_NODE));
  123.    if(!pNode){
  124.       perror("malloc");
  125.       return (char*)NULL;
  126.    }
  127. /*
  128.  * Have we reached the limit of the buffer?
  129.  */
  130.    if(lHead.n_lines == lHead.max_lines)
  131.       pOldData = IDL_RPCOutListDequeue(); /* remove last node */
  132.    pNode->data = pData;
  133.    pNode->next = lHead.head;
  134.    lHead.head = pNode;
  135.    if(lHead.n_lines > 0)
  136.       pNode->next->prev = pNode;
  137.    else 
  138.       lHead.tail = pNode;
  139.    lHead.n_lines++;
  140.  
  141.    return pOldData; 
  142. }
  143. /*************************************************************
  144.  * IDL_RPCToutFunc()
  145.  * 
  146.  * Purpose:
  147.  *     This function is used to capture the output from IDL. This 
  148.  *     function is used via the functions IDL_ToutPush() & IDL_ToutPop().
  149.  */
  150. IDL_TOUT_OUTF IDL_RPCToutFunc( int flags, char *buf, int n)
  151. {
  152.  
  153.    char              szBuffer[IDL_RPC_MAX_STRLEN];
  154.    IDL_RPC_LINE_S   *pOldData;
  155.    IDL_RPC_LINE_S   *psLine;
  156.    
  157. /*
  158.  * Create a new line struct 
  159.  */
  160.    psLine = (IDL_RPC_LINE_S*)malloc((unsigned)sizeof(IDL_RPC_LINE_S));
  161.    if(!psLine){
  162.       perror("malloc");
  163.       return;
  164.    }
  165. /*
  166.  * Copy over the string. Make sure that the length of the buffer is 
  167.  * not exceeded.
  168.  */
  169.    n = (n < IDL_RPC_MAX_STRLEN-1 ? n : IDL_RPC_MAX_STRLEN-1);
  170.    bcopy(buf, (char*)szBuffer, n);
  171.    szBuffer[n] = '\0';        /* null terminate string */
  172.      
  173.    psLine->buf = (char*)strdup(szBuffer); /* duplicate the string. */
  174.    psLine->flags = flags;
  175.  
  176. /*
  177.  * Add the new line to the buffer
  178.  */   
  179.    pOldData = (IDL_RPC_LINE_S*)IDL_RPCOutListAdd((char*)psLine);
  180.  
  181.    if(pOldData != (IDL_RPC_LINE_S*)NULL){
  182.    /*
  183.     * The line buffer is full so a data element was dequeued
  184.     */
  185.       free(pOldData->buf);
  186.       free((char*)pOldData);
  187.    }
  188. } /* IDL_RPCToutFunc */
  189. /*************************************************************
  190.  * IDL_RPCOutLIstCnt()
  191.  *
  192.  * Purpose:
  193.  *     Used to get the number of lines currently in the list.
  194.  *
  195.  */
  196. int IDL_RPCOutListCnt(void)
  197. {
  198.    return lHead.n_lines;
  199. }
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.